<template>
	<el-dialog
		:title="cuFromTitle"
		v-model="cuVisible"
		width="600px"
		top="0"
		:destroy-on-close="true"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		:show-close="false"
	>
		<div>
			<el-form
				ref="form"
				:model="form"
				label-width="120px"
			>
				<el-row>
					<el-col :span="24">
						<el-form-item label="成团人数" prop="tuan_num">
							<el-input-number v-model="form.tuan_num"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="是否虚拟成团" prop="is_fictitious">
							<el-radio-group v-model="form.is_fictitious">
								<el-radio :label="0">否</el-radio>
								<el-radio :label="1">是</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="超团策略" prop="exceed_policy">
							<el-radio-group v-model="form.exceed_policy">
								<el-radio v-for="item in loopOptions" :key="item.value" :label="item.value">{{item.label}}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="自动关团时间" prop="timeout">
							<div>
								<el-input-number v-model="form.timeout" :min="3600"></el-input-number>
								<div class="form-item-help">单位：秒 ,1小时起, 一天是 86400 秒</div>
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="支付超时时间" prop="close_time">
							<div>
								<el-input-number v-model="form.close_time" :min="300"></el-input-number>
								<div class="form-item-help">单位：秒 ,5分钟起, 默认 300 秒 5 分钟</div>
							</div>
						</el-form-item>
					</el-col>
				</el-row>

			</el-form>
		</div>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="hideForm" :loading="btnLoad" size="mini">取消</el-button>
				<el-button type="primary" :loading="btnLoad" size="mini" @click="cuFormSubmit"
				>提交</el-button
				>
			</div>
		</template>
	</el-dialog>
</template>

<script>
import { ElMessage } from "element-plus";

export default {
	name: "shop-goods-activity-tuan-rule",
	components: {},
	props: {
		baseUrl: { type: String, default: "" }
	},
	emits: ["success"],
	data() {
		return {
			// 如果为0时为添加
			activityId: 0,
			btnLoad: false,
			cuVisible: false,
			activity_type: 0,
			title: 0,
			form: {
				tuan_num: 2,
				is_fictitious: 0,
				exceed_policy: 1,
				timeout: 86400,
				close_time: 300
			},
			loopOptions: [
				{
					label: "允许超卖",
					value: 1
				},
				{
					label: "超卖自动开新团",
					value: 2
				},
				{
					label: "禁止超卖",
					value: 3
				},
			],
			ruleDatas: [],
			oneRuleDatas: [
				{
					threshold_num: 0,
					discount_num: 0
				}
			]
		};
	},
	computed: {
		cuFromTitle() {
			return "《" + this.title + "》团购规则";
		}
	},
	methods: {
		hideForm() {
			this.cuVisible = false;
		},
		show(_activityId, _activity_type, _title) {
			this.oneRuleDatas = [{ threshold_num: 0, discount_num: 0 } ];
			this.ruleDatas = [];
			this.getData(_activityId);
			this.activityId = _activityId;
			this.activity_type = _activity_type;
			this.title = _title;
			this.cuVisible = true;
		},
		getData(_activityId) {
			this.$API.shop.tuanActivity
				.getRule({ activity_id: _activityId })
				.then((res) => {
					console.log(res);
					if (res.code === 0) {
						this.form = res.data.rules;
					}
				})
				.catch((err) => {
					ElMessage.error("失败," + err);
				});
		},
		cuFormSubmit() {
			this.btnLoad = true;

			setTimeout(() => {
				this.btnLoad = false;
			}, 10000);

			let submitData = {
				activity_id: this.activityId,
				rules: {
					...this.form
				}
			};

			this.$API.shop.tuanActivity
				.setRule(submitData)
				.then((res) => {
					if (res.code === 0) {
						ElMessage.success("提交成功");
						this.formSuccess();
					}
					else {
						this.$message.error(res.message);
					}
				})
				.catch((err) => {
					ElMessage.error("失败," + err);
				});
		},
		formSuccess() {
			this.btnLoad = false;
			this.cuVisible = false;
			this.$emit("success");
		},
		formFail() {
			this.btnLoad = false;
		}
	}
};
</script>

<style scoped lang="scss">

</style>
